package rexsee.multimedia;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.media.AudioManager;

public class RexseeVideoPlayer implements JavascriptInterface {

	public static final String INTERFACE_NAME = "VideoPlayer";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeVideoPlayer(childBrowser);
	}

	private final Browser mBrowser;
	private final Context mContext;
	private VideoDialog mVideoDialog = null;

	public RexseeVideoPlayer(Browser browser) {
		mBrowser = browser;
		mContext = browser.getContext();
	}

	private boolean checkStatus() {
		if (mVideoDialog == null || mVideoDialog.videoView.mediaPlayer == null) {
			mBrowser.exception(getInterfaceName(), "The video player is not started, please call start().");
			return false;
		} else {
			return true;
		}
	}

	//JavaScript Interface

	public boolean start(String url, String style, boolean looping) {
		stop();
		url = mBrowser.urlListeners.getAbsoluteUrl(url);
		mVideoDialog = new VideoDialog(mBrowser, null, url, style, looping);
		mVideoDialog.start();
		return true;
	}
	public boolean stop() {
		if (mVideoDialog == null) return true;
		try {
			if (mVideoDialog.videoView.mediaPlayer != null) {
				mVideoDialog.videoView.mediaPlayer.stop();
				mVideoDialog.videoView.mediaPlayer.release();
				mVideoDialog.videoView.mediaPlayer = null;
			}
			mVideoDialog.dismiss();
			mVideoDialog = null;
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean pause() {
		if (!checkStatus()) return false;
		if (!mVideoDialog.videoView.mediaPlayer.isPlaying()) return true;
		try {
			mVideoDialog.videoView.mediaPlayer.pause();
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean resume() {
		if (!checkStatus()) return false;
		try {
			mVideoDialog.videoView.mediaPlayer.start();
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}
	public boolean seekTo(int milliseconds) {
		if (!checkStatus()) return false;
		try {
			mVideoDialog.videoView.mediaPlayer.seekTo(milliseconds);
			return true;
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return false;
		}
	}

	public int getDuration() {
		if (!checkStatus()) return -1;
		try {
			return mVideoDialog.videoView.mediaPlayer.getDuration();
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return -1;
		}
	}
	public int getCurrentPosition() {
		if (!checkStatus()) return -1;
		try {
			return mVideoDialog.videoView.mediaPlayer.getCurrentPosition();
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return -1;
		}
	}
	public int getVideoWidth() {
		if (!checkStatus()) return -1;
		try {
			return mVideoDialog.videoView.mediaPlayer.getVideoWidth();
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return -1;
		}
	}
	public int getVideoHeight() {
		if (!checkStatus()) return -1;
		try {
			return mVideoDialog.videoView.mediaPlayer.getVideoHeight();
		} catch (Exception e) {
			mBrowser.exception(getInterfaceName(), e);
			return -1;
		}
	}
	public boolean isPlaying() {
		if (!checkStatus()) return false;
		return mVideoDialog.videoView.mediaPlayer.isPlaying();
	}
	public boolean isLooping() {
		if (!checkStatus()) return false;
		return mVideoDialog.videoView.mediaPlayer.isLooping();
	}

	public boolean setVolume(float volume) {
		if (volume < 0 || volume > 1) {
			mBrowser.exception(getInterfaceName(), "The volumn must between 0 and 1.");
			return false;
		}
		AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
		mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, Math.round((mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * volume)), 0);
		return true;
	}

}
